﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillDate();
            }
        }
        private DataTable Data = new DataTable();

        private DataTable date
        {
            get
            {
                if (Data.Rows.Count > 0)
                {
                    return Data;
                }
                else
                {
                    var sql = "select * from Articles1";
                    var dt = DbHelper.GetDatatable(sql);

                    Data = dt;

                    return Data;
                }
            }
        }


        private void FillDate()
        {
            var sql = "select * from Articles1";
            var dt = DbHelper.GetDatatable(sql);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

      

        private string GetValues(int rowIndex, int v)
        {
            var cell = GridView1.Rows[rowIndex].Cells[v];
            var values = cell.Controls.Count > 0 ? ((TextBox)cell.Controls[0]).Text : cell.Text;

            return values;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var da = this.date.Copy();
            da.Rows.Add(da.NewRow());

            Data = da;

            GridView1.EditIndex = da.Rows.Count - 1;
            GridView1.DataSource = Data;
            GridView1.DataBind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillDate();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillDate();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetValues(e.RowIndex, 0);
            var isOk = int.TryParse(idString, out int id);

            var title = GetValues(e.RowIndex, 1);
            var content = GetValues(e.RowIndex, 2);
            var author = GetValues(e.RowIndex, 3);
            if (isOk)
            {
                var sql = string.Format("update Articles1 set Title='{0}',Content='{1}',Author='{2}' where Id={3}", title, content, author, id);
                DbHelper.add(sql);
            }
            else
            {
                var sql = string.Format("insert into Articles1 (Title,Content,Author) values('{0}','{1}','{2}')", title, content, author);
                DbHelper.add(sql);
            }

            GridView1.EditIndex = -1;
            FillDate();

        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetValues(e.RowIndex, 0);
            var sql = string.Format("delete from Articles1 where Id ={0}", id);
            DbHelper.add(sql);
            GridView1.EditIndex = -1;
            FillDate();
        }
    }
}